layui.config({
    base: Think.__JS__ + '/',
    version: '101100'
}).use(['table', 'laypage', 'common'], function () {
    var table = layui.table
        ,defaultFunc = function () {} // 默认函数
    // 各事件
    var onToolbar = $('script[role="onToolbar"]');
    var onCheckbox = $('script[role="onCheckbox"]');
    var onEdit = $('script[role="onEdit"]');
    var onRow = $('script[role="onRow"]');
    var onTool = $('script[role="onTool"]');
    var onSort = $('script[role="onSort"]');

    $('script[role="table"]').each(function (index, elem) {
        var str = $(this).html(), opt = {};
        if (str.length > 0)
            opt = window.ICY8.str2json(str);
        var option = {
            id: '_table'
            , elem: '#_table'
            , page: true
            , loading: true
            , method:'post'
            ,request: {
                pageName: 'p' //页码的参数名称，默认：page
            }
            , text: {
                none: '暂无相关数据',
            }
        };
        option.limit = 10;
        option.limits = [];
        var i = 0;
        $.extend(option, opt);
        // 自动计算分页
        if (option.limits.length>0) {
            while (i < option.limits.length) {
                option.limits[i] = (i + 1) * option.limit;
                i++;
            }
        } else {
            while (i < 10) {
                option.limits[i] = (i + 1) * option.limit;
                i++;
            }
        }
        if (opt.id && !opt.elem) {
            option.elem = '#' + option.id;
        }
        var handle = table.render(option);
        // 各事件
        option.onToolbar = window.ICY8.str2json($(onToolbar[index]).html());
        option.onCheckbox = window.ICY8.str2json($(onCheckbox[index]).html());
        option.onEdit = window.ICY8.str2json($(onEdit[index]).html());
        option.onRow = window.ICY8.str2json($(onRow[index]).html());
        option.onTool = window.ICY8.str2json($(onTool[index]).html());
        option.onSort = window.ICY8.str2json($(onSort[index]).html());

        table.on('toolbar(' + option.id + ')', (typeof option.onToolbar == 'function') ? option.onToolbar : defaultFunc);
        table.on('checkbox(' + option.id + ')', (typeof option.onCheckbox == 'function') ? option.onCheckbox : defaultFunc);
        table.on('edit(' + option.id + ')', (typeof option.onEdit == 'function') ? option.onEdit : defaultFunc);
        table.on('row(' + option.id + ')', (typeof option.onRow == 'function') ? option.onRow : defaultFunc);
        table.on('tool(' + option.id + ')', (typeof option.onTool == 'function') ? option.onTool : defaultFunc);
        table.on('sort(' + option.id + ')', (typeof option.onSort == 'function') ? option.onSort : defaultFunc);
    });
});